#include "async_client.h"
#include "async_session.h"
#include "async_server.h"
#include "signal_dispatcher.h"
#include "asio_timeout_wrapper.h"
int main() {
    asio::io_context io;
    SignalDispatcher dispatcher(io);

    // 启动客户端
    auto client = std::make_shared<AsyncClient>(io);
    dispatcher.connect_async(client->signal_connected, [&/*if client, will memory leak!*/](){
        std::cout << "[Client] 已连接到服务器" << std::endl;
        client->send("Hello Server!");
    });

    client->signal_connected.connect([&]() {
        std::cout << "[Client] 已连接到服务器" << std::endl;
        client->send("Hello Server!");
    });

    dispatcher.connect_async(client->signal_message_received, [](const std::string& msg){
        std::cout << "[Client] 收到服务器回显: " << msg << std::endl;
    });

    client->connect("127.0.0.1", 12345);
    std::cout << "client connect over" << std::endl;

    //auto work = asio::make_work_guard(io);

    int count = 0;
    auto timeout1 = std::make_shared<AsioWrapper::Timeout>(io);
    timeout1->start(std::chrono::seconds(1), [&count]() {
        std::cout << "Tick: " << count << std::endl;
        count++;
        return count <= 5;
    });
//    }, true);

    auto timeout2 = std::make_shared<AsioWrapper::Timeout>(io);
    timeout2->start_once(std::chrono::seconds(3), [&]() {
        timeout1->stop();
    });

    {
        auto timeout = std::make_shared<AsioWrapper::Timeout>(io);
        timeout->start_once(std::chrono::seconds(3), [&]() {
//            client->stop();
            io.stop();
        });
    }

    io.run();
    std::cout << "exit " << std::endl;
}

